package views;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import read.prjx.ReadInfo;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Vector;

public class Jframe extends JFrame implements ActionListener {
    JButton openBtn = new JButton("打开切割包");
    JButton saveBtn = new JButton("保存");
    JButton printBtn = new JButton("标签打印");
    JTable table = new JTable();
    Vector<Vector<String>> tabData = new Vector<>();
    JPanel btnPanel = new JPanel();
    String path = "";
    JPanel panel = new JPanel();
    Vector<String> title = new Vector<String>();// 列名
    ReadInfo readPrjx = new ReadInfo();
    DefaultTableModel tableModel = null;
    public Jframe() {
        Vector<Vector<Object>> o;

        setSize(1024, 768);
        setLayout(new BorderLayout());
        panel.setLayout(new BorderLayout());
        openBtn.addActionListener(this);
        printBtn.addActionListener(this);
        title.add("序号");
//        title.add("订单号");
        title.add("客户名称");
        title.add("产品名称");
        title.add("宽度");
        title.add("高度");
        title.add("数量");
        title.add("备注");
        title.add("编号");
        title.add("版图号");
        title.add("玻璃颜色");
        title.add("UUID");
        panel.add(new JScrollPane(table));
        btnPanel.add(openBtn);
        btnPanel.add(printBtn);
        printBtn.setEnabled(false);
        add(panel, BorderLayout.CENTER);
        add(btnPanel, BorderLayout.NORTH);
        setLocationRelativeTo(null);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String buttonName = e.getActionCommand();
//        if (tableModel != null){
//            tableModel.setRowCount( 0 );
//        }

        if (buttonName.equals("打开切割包")) {
            javax.swing.filechooser.FileSystemView fsv = javax.swing.filechooser.FileSystemView.getFileSystemView();


            JFileChooser fileChooser = new JFileChooser("C:\\PERFECT6\\CNC");
            fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            int returnVal = fileChooser.showOpenDialog(openBtn);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                path = fileChooser.getSelectedFile().toString();

                if (path.contains("prjx")) {
                    tabData = readPrjx.readPrjx(path);
                    tableModel = new DefaultTableModel(tabData, title){
                        public boolean isCellEditable(int row,int column){
                            return false;
                        }
                    };
                    table.setModel(tableModel);
                    TableColumn firsetColumn = table.getColumnModel().getColumn(0);
                    firsetColumn.setPreferredWidth(30);
                    firsetColumn.setMaxWidth(30);
                    firsetColumn.setMinWidth(30);
                    table.getTableHeader().setFont(new Font("宋体", Font.BOLD, 12));
                    table.setFont(new Font("微软雅黑", Font.PLAIN, 12));
                    table.setRowHeight(30);
                    setTitle(path);
                    repaint();
                    revalidate();
                    printBtn.setEnabled(true);
                } else {
                    JOptionPane.showMessageDialog(panel, "你打开的不是切割包请重新选择", "温馨提示：", JOptionPane.WARNING_MESSAGE);
                }
            }
        } else if (buttonName.equals("标签打印")) {
            HSSFWorkbook wb = new HSSFWorkbook();
            Vector<String> xlsTitle = new Vector<String>();// 列名
            xlsTitle.add("序号");
//            xlsTitle.add("订单号");
            xlsTitle.add("客户名称");
            xlsTitle.add("产品名称");
            xlsTitle.add("宽度");
            xlsTitle.add("高度");
            xlsTitle.add("数量");
            xlsTitle.add("备注");
            xlsTitle.add("编号");
            xlsTitle.add("版图号");
            xlsTitle.add("玻璃颜色");
            xlsTitle.add("UUID");
            xlsTitle.add("ID");
            xlsTitle.add("CLASSIFY");
            xlsTitle.add("BARCODE");
            xlsTitle.add("ROTATE");
            HSSFSheet sheet = wb.createSheet("标签打印");
            HSSFRow row = sheet.createRow(0);
            for (int i = 0; i < xlsTitle.size(); i++) {
                row.createCell(i).setCellValue(xlsTitle.get(i));

            }
            for (int i = 0; i < tabData.size(); i++) {
                row = sheet.createRow(i+1);
                for (int j = 0; j < tabData.get(i).size(); j++) {
                    row.createCell(j).setCellValue(tabData.get(i).get(j));
                }
            }
            try {
                FileOutputStream fileOutputStream = new FileOutputStream("C:\\123\\table.xls");
                wb.write(fileOutputStream);
                fileOutputStream.close();
                wb.close();

                JOptionPane.showMessageDialog(panel, "一共生成："+ tabData.size()+"个标签", "温馨提示：", JOptionPane.WARNING_MESSAGE);

            } catch (IOException fileNotFoundException) {
                fileNotFoundException.printStackTrace();
            }
        }
    }
}